package com.starmark.core.framework.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starmark.core.framework.util.Query;
import com.starmark.core.framework.vo.PageQueryVo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;


/**
 * 业务逻辑层基类
 *
 * @author starmark
 * @version 2018年5月20日 下午3:19:19
 */
@Slf4j
public abstract class AbstractBaseService<Mapper extends SuperMapper<T>, T extends BaseModel> extends ServiceImpl<Mapper,T> implements
        BaseService<T> {


    @Override
    public Page<T> queryPage(PageQueryVo pageQueryVo){
        Page<T> page = new Page<>(pageQueryVo.getCurrPage(), pageQueryVo.getLimit());
        QueryWrapper<T> eWrapper = new QueryWrapper<>();
        this.buildPageSearch(pageQueryVo.getParams(), eWrapper);
        IPage<T> pageResult = this.getBaseMapper().selectPage(page, eWrapper);
        page.setRecords(pageResult.getRecords());
        page.setTotal(pageResult.getTotal());
        return page;
    }

    /**
     * 根据参数分页查询
     */
    @Override
    public Page<T> query(Map<String, Object> params) {
        Page<T> page = new Query<T>(params).getPage();
        QueryWrapper<T> eWrapper = new QueryWrapper<>();
        this.buildPageSearch(params, eWrapper);
        IPage<T> pageResult = this.getBaseMapper().selectPage(page, eWrapper);
        page.setRecords(pageResult.getRecords());
        page.setTotal(pageResult.getTotal());
        return page;
    }

    /**
     * 构造分页的查询件条件
     *
     * @param params   参数
     * @param eWrapper 查询条件
     */

    protected void buildPageSearch(Map<String, Object> params, QueryWrapper<T> eWrapper) {
     params.forEach(eWrapper::eq);

    }







    @Override
    /* 根据参数查询 */
    public List<T> queryList(Map<String, Object> params) {
        QueryWrapper<T> eWrapper = new QueryWrapper<>();
        this.buildPageSearch(params, eWrapper);

        return this.getBaseMapper().selectList(eWrapper);
    }
}
